//
//  FSFoodChildVC.m
//  FieldStory
//
//  Created by itte on 2017/6/23.
//  Copyright © 2017年 funmain. All rights reserved.
//

#import "FSFoodChildVC.h"
#import "IngredientsCltCell.h"
#import "LineLayout.h"
#import "FSShoppingCartData.h"

@interface FSFoodChildVC ()<UICollectionViewDataSource,UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *lbNum;
@end

@implementation FSFoodChildVC

- (void)viewDidLoad {
    [super viewDidLoad];
    LineLayout *layout = [[LineLayout alloc] init];
    [self.cltContentView setCollectionViewLayout:layout];
    self.cltContentView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.cltContentView.dataSource = self;
    [self.cltContentView registerNib:[UINib nibWithNibName:@"IngredientsCltCell" bundle:nil] forCellWithReuseIdentifier:@"IngredientsCltCellID"];
}

-(void)reloadData
{
    [self.cltContentView reloadData];
}

#pragma mark - 按钮点击
- (IBAction)btnChangeNumClick:(UIButton *)sender
{
    int value = [self.lbNum.text intValue];
    if (sender.tag == 600) {
        NSInteger num = 0;
        for (FSShopListModel *shop in [FSShoppingCartData sharedInstance].boxArray) {
            num += shop.payNum;
        }
        if (value+1>11-num) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"箱子不能装那么多个" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
            [alert show];
            return ;
        }
        self.lbNum.text = [NSString stringWithFormat:@"%d", ++value];
        return ;
    }
    if (value <= 1) {
        self.lbNum.text = @"1";
    }
    else{
        self.lbNum.text = [NSString stringWithFormat:@"%d", --value];
    }
}
- (IBAction)btnAddClick
{
    NSInteger num = 0;
    for (FSShopListModel *shop in [FSShoppingCartData sharedInstance].boxArray) {
        num += shop.payNum;
    }
    if (num == 11) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"已满箱，请结算后在选购吧！" delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"确定",nil];
        [alert show];
        return ;
    }
    NSInteger pageIndex = self.cltContentView.contentOffset.x/SCREEN_WIDTH+0.5;
    NSLog(@"--%ld",pageIndex);
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(addProductToBox)]) {
        NSInteger count = [self.lbNum.text integerValue];
        FSShopListModel *shop = [FSShoppingCartData sharedInstance].dataArray[pageIndex];
        for (FSShopListModel *shopInBox in [FSShoppingCartData sharedInstance].boxArray) {
            if (shop.ID == shopInBox.ID) {
                shopInBox.payNum = shopInBox.payNum + count;
                [self.delegate addProductToBox];
                self.lbNum.text = @"1";
                return ;
            }
        }
        shop.payNum = count;
        [[FSShoppingCartData sharedInstance].boxArray addObject:shop];
        [self.delegate addProductToBox];
        self.lbNum.text = @"1";
    }
}


#pragma mark - UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return [FSShoppingCartData sharedInstance].dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IngredientsCltCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"IngredientsCltCellID" forIndexPath:indexPath];
    FSShopListModel *shop = [FSShoppingCartData sharedInstance].dataArray[indexPath.item];
    [cell.imgShop sd_setImageWithURL:[NSURL URLWithString:shop.images] placeholderImage:[UIImage imageNamed:@"foster_my-farm_pic5_Tomato"]];
    cell.lbName.text = shop.name;
    cell.lbWeight.text = [shop.weight addString:@"kg"];
    cell.lbPrice.text = [@"￥" addString:shop.vip_price];
    cell.lbNormelPrice.text = [@"￥" addString:shop.price];
    return cell;
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [self.cltContentView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:YES];
    return NO;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"-- %ld --",indexPath.row);
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
}


#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    NSLog(@"buttonIndex: %ld",buttonIndex);
    if (buttonIndex == 1 && self.delegate && [self.delegate respondsToSelector:@selector(gotoPrePay)]) {
        [self.delegate gotoPrePay];
    }
}
@end
